package com.dl.tt.control;

import com.dl.tt.pojo.Dept;
import com.dl.tt.pojo.Emp;
import com.dl.tt.service.EmpService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @author 47
 * @version 1.0
 */
@Controller
public class AdminControl {
    @Autowired
    private EmpService service;
    // 管理员登录
    @GetMapping("/admin/login.do")
    public String login(){
        return "adminLogin";
    }
    // 跳转到 修改密码
    @GetMapping("/admin/adminChangePassword.do")
    public String adminChangePassword(){
        return "adminChangePassword";
    }
    // 密码修改成功后，跳转到 员工审批页面
    @PostMapping("/admin/adminChangePassword.do")
    public String adminChangePassword(String password, ModelMap map, HttpSession session){
        Object obj = session.getAttribute("accountName");
        String accountName = obj.toString();
        Integer result = this.service.changePassword(accountName, password);
        Emp emp = this.service.findEmpByAccountNameAndPassword(accountName, password);
        session.setAttribute("emp",emp);
        return "employeeApprove";
    }
    // 管理员登录 之后 跳转到 人员管理 员工审批
    @RequestMapping("/admin/employeeApprove.do")
    public String employeeApprove(String accountName, String password,
                                  ModelMap map, HttpSession session){
        Emp emp = this.service.findEmpByAccountNameAndPassword(accountName, password);
        // map.put("accountName",accountName);
        List<Emp> empList = this.service.findAllRegister();
        session.setAttribute("accountName",accountName);
        session.setAttribute("password",password);
        session.setAttribute("emp",emp);
        session.setAttribute("empList",empList);
        System.out.println("#EmpControl#login"+emp);
        if (Objects.isNull(emp)){
            return "adminLogin";
        }
        //map.put("empList",empList);
        return "employeeApprove";
    }
    // 员工审批 通过按钮 操作
    @RequestMapping ("/admin/pass.do")
    @ResponseBody
    public String pass(@RequestParam("empNo") String empNo){
        //System.out.println("EmpControl#pass#"+empNo);
        Integer result = this.service.passEmp(Integer.parseInt(empNo));
        return String.valueOf(result);
    }

    // 员工审批 删除按钮 操作
    @PostMapping ("/admin/delete.do")
    @ResponseBody
    public String delete(@RequestParam("empNo") String empNo){
        Integer result = this.service.deleteEmp(Integer.parseInt(empNo));
        return String.valueOf(result);
    }



    // 部门管理
    @RequestMapping("/admin/department.do")
    public String department(ModelMap map,HttpSession session){
        List<Dept> deptList = this.service.findDept();
        session.setAttribute("deptList",deptList);
        return "department";
    }

    // 添加部门
    @RequestMapping("/admin/addDept.do")
    public String addDept(@RequestParam String deptName,HttpSession session){
        Integer result = this.service.addDept(deptName);
        List<Dept> deptList = this.service.findDept();
        session.setAttribute("deptList",deptList);
        return "department";
    }
    // 员工注册 跳转到页面
    @GetMapping("/admin/register.do")
    public String register(){
        return "register";
    }
    // 员工注册 表单提交
    @PostMapping("/admin/register.do")
    public String register(Emp emp,HttpSession session){
        Integer result = this.service.addEmp(emp);
        List<Emp> empList = this.service.findAllRegister();
        session.setAttribute("empList",empList);
        return "employeeApprove";
    }

    // 搜索员工
    @GetMapping("admin/search.do")
    public ModelAndView search(
            @RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
            ModelAndView modelAndView){
        Emp emp = new Emp();
        PageInfo<Emp> empPageInfo = this.service.findAllByEmpAndPage(pageNum, pageSize, emp);
        modelAndView.setViewName("search");
        modelAndView.addObject("empPageInfo",empPageInfo);
        modelAndView.addObject("flag",emp.getFlag());
        // ModelMap modelMap = modelAndView.getModelMap();
        // modelMap.put("empPageInfo",empPageInfo);
        return modelAndView;
    }
    // 对员工进行查询，使用了分页
    @PostMapping("admin/search.do")
    public String search(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
            Emp emp,ModelMap map){
        PageInfo<Emp> empPageInfo = this.service.findAllByEmpAndPage(pageNum, pageSize, emp);
        map.put("empPageInfo",empPageInfo);
        map.put("flag",emp.getFlag());
        map.put("nameLike",emp.getEmployeeName());
        map.put("accountLike",emp.getAccountName());
        return "search";
    }

    @PostMapping("admin/page.do")
    @ResponseBody
    public List<Emp> page(
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            Emp emp,ModelMap map){
        System.out.println("------------------------------------------------");
        PageInfo<Emp> empPageInfo = this.service.findAllByEmpAndPage(pageNum, pageSize, emp);
        List<Emp> empList = empPageInfo.getList();
        map.put("empPageInfo",empPageInfo);
        map.put("flag",emp.getFlag());
        return empList;
    }

    @GetMapping ("/admin/delete.do")
    public String delete2(String empNo){
        Integer result = this.service.deleteEmp(Integer.parseInt(empNo));
        return "search";
    }

    // 部门管理中 编辑 ：修改
    @PostMapping("/admin/deptUp.do")
    @ResponseBody
    public String deptUp(@RequestParam("deptNo") Integer deptNo,@RequestParam("deptName") String deptName){
        this.service.changeDeptName(deptNo,deptName);
        return deptName;
    }
}
